package com.liangchen.asstability;

import androidx.annotation.NonNull;

import com.liangchen.astools.utils.AsLog;

public class JEMonitor {
    private final String TAG = "JEMonitor";

    private Thread.UncaughtExceptionHandler mDefaultUEH;
    private JEMonitor() {
    }

    public static class InnerHolder {
        public static final JEMonitor instance = new JEMonitor();
    }

    public static JEMonitor getInstance() {
        return InnerHolder.instance;
    }

    public void init() {
        startMonitor();
        AsLog.i(TAG, "JEMonitor init finished.");
    }

    private void startMonitor() {
        mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
        CrashHandler ourUEH = new CrashHandler();
        Thread.setDefaultUncaughtExceptionHandler(ourUEH);
    }

    private class CrashHandler implements Thread.UncaughtExceptionHandler {

        @Override
        public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
            AsLog.e(TAG, ">>>>>>>>>>>>>>>>>>>>");
            AsLog.e(TAG, "Catch a JavaExeption");
            AsLog.e(TAG, "Exption:"+ e.toString());
            AsLog.e(TAG, "Message:"+ e.getMessage());
            AsLog.e(TAG, "Stack:"+ e.getStackTrace());
            AsLog.e(TAG, "<<<<<<<<<<<<<<<<<<<<");
            mDefaultUEH.uncaughtException(t, e);
        }
    }
}